﻿using AutoMapper;
using FakeXiecheng.API.Dtos;
using FakeXiecheng.API.Models;
using FakeXiecheng.API.Services;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace FakeXiecheng.API.Controllers
{
    [ApiController]
    [Route("api/[controller]/{TouristRouteId}/pictures")]
    public class TouristRoutePicturesController : ControllerBase
    {
        private ITouristRouteRepository _touristRouteRepository;
        private readonly IMapper _mapper;
        public TouristRoutePicturesController(ITouristRouteRepository touristRoute, IMapper mapper)
        {
            _touristRouteRepository = touristRoute;
            _mapper = mapper;
        }


        /// <summary>
        /// 获取同一个id的所有图片资源
        /// </summary>
        /// <param name="touristRouteId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetTouristRoutePicture(Guid touristRouteId)
        {
            if (!await _touristRouteRepository.TouristRoutesExistAsync(touristRouteId))
            {
                return NotFound("未找到该路线");
            }

            //找到了 数据库拿数据
            var touristRoute = await _touristRouteRepository.GetTouristRoutePictureByTouristRouteIdAsync(touristRouteId);
            if (touristRoute == null || touristRoute.Count() < 0)
            {
                return NotFound("没有图片信息");
            }
            var dto = _mapper.Map<IEnumerable<TouristRoutePicturesDTO>>(touristRoute);


            return Ok(dto);
        }

        /// <summary>
        /// 根据父id和图片id获取图片
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="pictureId"></param>
        /// <returns></returns>
        [HttpGet("{pictureId}", Name = "GetTouristRoutePicture")]
        public async Task<IActionResult> GetTouristRoutePicture(Guid touristRouteId, int pictureId)
        {
            if (!await _touristRouteRepository.TouristRoutesExistAsync(touristRouteId))
            {
                return NotFound("未找到该条路线");
            }

            var picture = await _touristRouteRepository.GetTouristRoutePictureAsync(pictureId);
            if (picture == null)
            {
                return NotFound("没有找到任何图片");
            }

            var dto = _mapper.Map<TouristRoutePicturesDTO>(picture);

            return Ok(dto);
        }


        /// <summary>
        /// 创建路线图片
        /// </summary>
        /// <param name="TouristRouteId"></param>
        /// <param name="pictureDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddTouristRoutePicture([FromRoute] Guid TouristRouteId, [FromBody] TouristRoutePictureForCreateDTO pictureDTO)
        {
            if (!await _touristRouteRepository.TouristRoutesExistAsync(TouristRouteId))
            {
                return NotFound("未找到该条路线");
            }

            var touristRoutePicture = _mapper.Map<TouristRoutePicture>(pictureDTO);
            _touristRouteRepository.AddTouristRoutePicture(TouristRouteId, touristRoutePicture);
            _touristRouteRepository.SaveAsync();
            var resultDTO = _mapper.Map<TouristRoutePicturesDTO>(touristRoutePicture);

            return CreatedAtRoute("GetTouristRoutePicture",
                new
                {
                    TouristRouteId = resultDTO.TouristRouteId,
                    pictureId = resultDTO.Id
                },
                resultDTO);
        }


        /// <summary>
        /// 删除嵌套资源的子资源
        /// </summary>
        /// <param name="touristRouteId"></param>
        /// <param name="pictureId"></param>
        /// <returns></returns>
        [HttpDelete("{pictureId}")]
        public async Task<IActionResult> DeleteTouristRoutePictureById([FromRoute] Guid touristRouteId, [FromRoute] int pictureId)
        {
            if (!await _touristRouteRepository.TouristRoutesExistAsync(touristRouteId))
            {
                return NotFound("旅游路线不存在，资源删除失败！");
            }

            var picture = await _touristRouteRepository.GetTouristRoutePictureAsync(pictureId);
            _touristRouteRepository.DeleteTouristRoutePicture(picture);
            await _touristRouteRepository.SaveAsync();

            return NoContent();
        }

    }
}
